/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AccessPermission;

import Module.ModuleBean;
import SubModule.SubModuleBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author aamod
 */
public class UserAcessPermissionServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session=request.getSession();
        try {
            Vector<Object> vector = new Vector<Object>();
            int uid = Integer.parseInt(request.getParameter("uid"));
            String[] withdraw = request.getParameterValues("check");
            if (withdraw != null) {
                int i = AccessPermission.Query.delete(uid);
                if (i != 0) {
                    response.sendRedirect("/RIT/Services/HR/HR.jsp");
                }
            } else {
                int i = AccessPermission.Query.delete(uid);
                if (i != 0) {
                    Vector<Object> list = Module.Query.viewAll();
                    Iterator it = list.iterator();
                    while (it.hasNext()) {
                        ModuleBean mb = (ModuleBean) it.next();
                        String value[] = request.getParameterValues(mb.getModuleName());
                        if (value != null) {
                            if (value[0].equals(mb.getModuleName())) {
                                Vector<Object> list1 = SubModule.Query.viewAll(mb.getModuleId());
                                Iterator it1 = list1.iterator();
                                while (it1.hasNext()) {
                                    SubModuleBean sb = (SubModuleBean) it1.next();
                                    UserAccessPermissionBean ub = new UserAccessPermissionBean();
                                    ub.setAcessStatus(1);
                                    ub.setModuleId(mb.getModuleId());
                                    ub.setSubModuleId(sb.getSubModuleId());
                                    ub.setUserId(uid);
                                    vector.add(ub);
                                }
                            } else {
                                Vector<Object> list1 = SubModule.Query.viewAll(mb.getModuleId());
                                Iterator it1 = list1.iterator();
                                while (it1.hasNext()) {
                                    int flag = 0;
                                    SubModuleBean sb = (SubModuleBean) it1.next();
                                    UserAccessPermissionBean ub = new UserAccessPermissionBean();
                                    dd:
                                    for (int j = 0; j < value.length; j++) {
                                        if (value[j].equals(sb.getSubModuleName())) {
                                            flag = 1;
                                            break dd;
                                        }
                                    }
                                    if (flag == 1) {
                                        ub.setAcessStatus(1);
                                        ub.setModuleId(mb.getModuleId());
                                        ub.setSubModuleId(sb.getSubModuleId());
                                        ub.setUserId(uid);
                                        vector.add(ub);
                                    } else {
                                        ub.setAcessStatus(0);
                                        ub.setModuleId(mb.getModuleId());
                                        ub.setSubModuleId(sb.getSubModuleId());
                                        ub.setUserId(uid);
                                        vector.add(ub);
                                    }
                                }
                            }
                        } else {
                            Vector<Object> list1 = SubModule.Query.viewAll(mb.getModuleId());
                            Iterator it1 = list1.iterator();
                            while (it1.hasNext()) {
                                SubModuleBean sb = (SubModuleBean) it1.next();
                                UserAccessPermissionBean ub = new UserAccessPermissionBean();
                                ub.setAcessStatus(0);
                                ub.setModuleId(mb.getModuleId());
                                ub.setSubModuleId(sb.getSubModuleId());
                                ub.setUserId(uid);
                                vector.add(ub);
                            }
                        }
                    }
                }
            }
            int temp = AccessPermission.Query.add(vector);
            if (temp != 0) {
                session.setAttribute("status", "Success");
                session.setAttribute("msg", "Access Permission has changed");
                response.sendRedirect("/RIT/Services/HR/HR.jsp");

            }
        } catch (Exception e) {
            session.setAttribute("status", "Success");
            session.setAttribute("msg", "Access Permission not changed");
            response.sendRedirect("/RIT/Services/HR/HR.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(UserAcessPermissionServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(UserAcessPermissionServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
